//
//  ResignView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/4/7.
//

import UIKit
import SnapKit
import SDWebImage
class ResignView: GreenNaviBarView {
    lazy var signatureImageView: UIImageView = {
        let view = UIImageView()
        view.layer.cornerRadius = Constant.BackViewCornerRadius
        view.layer.borderColor = UIColor(hexStr: "#292D3233").cgColor
        view.layer.borderWidth = 1
        view.clipsToBounds = true
        return view
    }()
    
    lazy var signedByText = ContentLabel(hexStr: "#292D32B2")
    lazy var signedDateText = ContentLabel(hexStr: "#292D32B2")
    lazy var reSignBtn = OperationButton(R.string.localizable.reSign())
    
    let activityView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView()
        view.color = .gray
        if #available(iOS 13.0, *) {
            view.style = .gray
        }else{
            view.style = .whiteLarge
        }
        return view
    }()
    override func setUpView() {
        self.backgroundColor = .white
        let contentBackView = UIView()
        contentBackView.drawCorner(byRoundingCorners: [.topLeft,.topRight])
        self.addSubview(contentBackView)
        contentBackView.snp.makeConstraints { make in
            make.top.equalTo(topBackView.snp.bottom).offset(-20)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(self.safeAreaLayoutGuide.snp.bottom).offset(Constant.BottomMargin)
        }
        contentBackView.addSubview(signatureImageView)
        signatureImageView.snp.makeConstraints { make in
            make.left.top.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(APPConfig.HeightForPortrait / 2)
        }
        //加载动画
        contentBackView.addSubview(activityView)
        activityView.snp.makeConstraints { make in
            make.width.height.equalTo(100)
            make.center.equalTo(signatureImageView)
        }
        //signedBy
        let signedByTitle = ContentLabel()
        signedByTitle.text = R.string.localizable.signedBy()
        contentBackView.addSubview(signedByTitle)
        signedByTitle.snp.makeConstraints { make in
            make.top.equalTo(signatureImageView.snp.bottom).offset(20)
            make.left.equalTo(signatureImageView)
            make.height.width.greaterThanOrEqualTo(0)
        }
        signedByText.numberOfLines = 0
        signedByText.textAlignment = .right
        contentBackView.addSubview(signedByText)
        signedByText.snp.makeConstraints { make in
            make.left.equalTo(signedByTitle.snp.right).offset(5)
            make.centerY.equalTo(signedByTitle)
            make.right.equalTo(signatureImageView)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //signed date
        let signedDateTitle = ContentLabel()
        signedDateTitle.text = "Date:"
        contentBackView.addSubview(signedDateTitle)
        signedDateTitle.snp.makeConstraints { make in
            make.top.equalTo(signedByTitle.snp.bottom).offset(12)
            make.left.equalTo(signatureImageView)
            make.height.width.greaterThanOrEqualTo(0)
        }
        contentBackView.addSubview(signedDateText)
        signedDateText.snp.makeConstraints { make in
            make.centerY.equalTo(signedDateTitle)
            make.right.equalTo(signatureImageView)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //re-sign btn
        contentBackView.addSubview(reSignBtn)
        reSignBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-10)
            make.right.left.equalTo(signatureImageView)
            make.height.equalTo(OperationButton.height)
        }
    }
    func setData(_ type: SignatureType){
        var signedBy: String = ""
        var signedDate: String = ""
        var imagePath: String = ""
        switch type {
        case .Installer, .InstallerAndDesiger:
            signedBy = JobDetailModelHelper.shared.jobDetailModel.signatures.installer_full_name
            signedDate = JobDetailModelHelper.shared.jobDetailModel.signatures.installer_signed_at
            imagePath = JobDetailModelHelper.shared.jobDetailModel.signatures.installer_signed_signature
        case .Designer:
            signedBy = JobDetailModelHelper.shared.jobDetailModel.signatures.designer_full_name
            signedDate = JobDetailModelHelper.shared.jobDetailModel.signatures.designer_signed_at
            imagePath = JobDetailModelHelper.shared.jobDetailModel.signatures.designer_signed_signature
        case .Owner:
            signedBy = JobDetailModelHelper.shared.jobDetailModel.signatures.owner_signed_name
            signedDate = JobDetailModelHelper.shared.jobDetailModel.signatures.owner_signed_at
            imagePath = JobDetailModelHelper.shared.jobDetailModel.signatures.owner_signed_signature
        case .Witness:
            signedBy = JobDetailModelHelper.shared.jobDetailModel.signatures.witness_signed_name
            signedDate = JobDetailModelHelper.shared.jobDetailModel.signatures.witness_signed_at
            imagePath = JobDetailModelHelper.shared.jobDetailModel.signatures.witness_signed_signature
        default:
            break
        }
        self.signedByText.text = signedBy
        self.signedDateText.text = signedDate
        if imagePath.contains("http"), let url = URL(string: imagePath){
            self.activityView.startAnimating()
            self.signatureImageView.sd_setImage(with: url, placeholderImage: nil, options: [SDWebImageOptions.scaleDownLargeImages]) {[weak self] (image, error, cashtype, url) in
                guard let strongSelf = self else { return  }
                strongSelf.activityView.stopAnimating()
                if let _ = image {
                    strongSelf.signatureImageView.contentMode = .scaleAspectFit
                }else{
                    strongSelf.signatureImageView.contentMode = .center
                    strongSelf.signatureImageView.image = UIImage(named: "edit_panelnumber_loadingfailed")
                }
            }
        }else if let image = UIImage(contentsOfFile: imagePath){
            self.signatureImageView.contentMode = .scaleAspectFit
            self.signatureImageView.image = image.generateThumbnailImage()
        }else{
            self.signatureImageView.contentMode = .center
            self.signatureImageView.image = UIImage(named: "edit_panelnumber_loadingfailed")
        }
        //installed不显示resign，其他状态才显示
        reSignBtn.isHidden = (JobDetailModelHelper.shared.jobDetailModel.job_status == PVDJobStatusValue.installed.rawValue ? true : false)
    }

}
